package com.gxa.mobile.service.impl;

import com.gxa.mobile.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * @author IKE <hp1041735092@outlook.jp>
 * @date 24/3/2024
 */

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserServiceImpl implements UserService {
    private final StringRedisTemplate stringRedisTemplate;
    private final String PHONE_PREFIX = "phone_number:";
    private final Random r = new Random();

    @Override
    public void verifyPhone(String phone) {
        /**
         * 生成验证码,将phone作为K,num作为V存入redis
         */
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int num = r.nextInt(10);
            sb.append(num);
        }
        String numStr = sb.toString();
        stringRedisTemplate.opsForValue().set(PHONE_PREFIX+phone,numStr);
        //...TODO:将验证码numStr作为参数发送给phone,需要短信验证功能
    }

    @Override
    public void verifyCode(String phone, String num) {
        /**
         * 通过传入的phone从redis获取对应的num值进行匹配
         * 成功则生成token,存入redis后返回
         */
        String strNum = stringRedisTemplate.opsForValue().get(PHONE_PREFIX + phone);

    }
}
